import java.util.HashSet;
import java.util.Set;

public class Hashex {
    class Solution {
        public boolean isHappy(int n) {
            int sum = 0;
            Set<Integer> set = new HashSet<>();
            while (sum!=1){
                sum=0;
                while(n!=0){
                    int m = n;
                    n=n%10;
                    sum += n*n;
                    n = m/10;
                }
                if(!set.isEmpty()&&set.contains(sum)){
                    return false;
                }
                set.add(sum);
                n=sum;
            }
            return true;
        }
    }
}
